package com.zhonghui.mes.controller;

import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.core.page.TableDataInfo;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.mes.domain.MesMaterialApply;
import com.zhonghui.mes.domain.vo.MesMaterialApplyVo;
import com.zhonghui.mes.service.IMesMaterialApplyService;
import com.zhonghui.response.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 领料申请Controller
 * 
 * @author zhonghui
 * @date 2022-05-21
 */
@Api(tags="制造执行MES-领料申请")
@RestController
@RequestMapping("/mes/apply")
public class MesMaterialApplyController extends BaseController
{
    @Autowired
    private IMesMaterialApplyService mesMaterialApplyService;

    /**
     * 查询领料申请列表
     */
    @ApiOperation("查询领料申请列表")
    @GetMapping("/list")
    public TableDataInfo<List<MesMaterialApplyVo>> list(MesMaterialApply mesMaterialApply)
    {
        startPage();
        List<MesMaterialApplyVo> list = mesMaterialApplyService.selectMesMaterialApplyList(mesMaterialApply);
        return getDataTable(list);
    }

    /**
     * 导出领料申请列表
     */
    @ApiOperation("导出领料申请列表")
    @Log(title = "领料申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesMaterialApply mesMaterialApply)
    {
        List<MesMaterialApplyVo> list = mesMaterialApplyService.selectMesMaterialApplyList(mesMaterialApply);
        ExcelUtil<MesMaterialApplyVo> util = new ExcelUtil<MesMaterialApplyVo>(MesMaterialApplyVo.class);
        util.exportExcel(response, list, "领料申请数据");
    }

    /**
     * 获取领料申请详细信息
     */
    @ApiOperation("获取领料申请详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<MesMaterialApplyVo> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(mesMaterialApplyService.selectMesMaterialApplyById(id));
    }

    /**
     * 新增领料申请
     */
    @ApiOperation("新增领料申请")
    @Log(title = "领料申请", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody MesMaterialApplyVo mesMaterialApplyVo)
    {
        mesMaterialApplyVo.setApplicantStatus(0);
        return BaseResult.success(mesMaterialApplyService.insertMesMaterialApply(mesMaterialApplyVo));
    }

    /**
     * 修改领料申请
     */
    @ApiOperation("修改领料申请")
    @Log(title = "领料申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody MesMaterialApplyVo mesMaterialApplyVo)
    {
        mesMaterialApplyVo.setApplicantStatus(0);
        mesMaterialApplyVo.setReviewer(null);
        mesMaterialApplyVo.setReviewerStatus(null);
        mesMaterialApplyVo.setReviewerDate(null);
        mesMaterialApplyVo.setReviewComments("");
        return BaseResult.success(mesMaterialApplyService.updateMesMaterialApply(mesMaterialApplyVo));
    }

    /**
     * 提交领料申请
     */
    @ApiOperation("提交领料申请")
    @Log(title = "领料申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody MesMaterialApplyVo mesMaterialApplyVo)
    {
        // 申请状态 0未提交 1待审核 2已审核
        mesMaterialApplyVo.setApplicantStatus(1);
        mesMaterialApplyVo.setApplicantDate(DateUtils.getNowDate());
        if(mesMaterialApplyVo.getId() != null) {
            // 修改提交
            MesMaterialApplyVo mesMaterialApply = mesMaterialApplyService.selectMesMaterialApplyById(mesMaterialApplyVo.getId());
            if (mesMaterialApply == null) {
                return BaseResult.error("此领料申请不存在，不能提交！");
            }
            mesMaterialApplyVo.setReviewer(null);
            mesMaterialApplyVo.setReviewerStatus(null);
            mesMaterialApplyVo.setReviewerDate(null);
            mesMaterialApplyVo.setReviewComments("");
            return BaseResult.success(mesMaterialApplyService.updateMesMaterialApply(mesMaterialApplyVo));
        } else {
            return BaseResult.success(mesMaterialApplyService.insertMesMaterialApply(mesMaterialApplyVo));
        }
    }

    /**
     * 领料申请审核
     */
    @ApiOperation("领料申请审核")
    @Log(title = "领料申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody MesMaterialApplyVo mesMaterialApplyVo)
    {
        mesMaterialApplyVo.setApplicantStatus(2);
        mesMaterialApplyVo.setReviewer(SecurityUtils.getUserId());
        mesMaterialApplyVo.setReviewerDate(DateUtils.getNowDate());
        return BaseResult.success(mesMaterialApplyService.updateMesMaterialApply(mesMaterialApplyVo));
    }

    /**
     * 删除领料申请
     */
    @ApiOperation("删除领料申请")
    @Log(title = "领料申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(mesMaterialApplyService.deleteMesMaterialApplyByIds(ids));
    }

    /**
     * 设置领料单检验状态
     */
    @ApiOperation("设置领料单检验状态")
    @Log(title = "领料申请", businessType = BusinessType.UPDATE)
    @PutMapping("/set/check")
    public BaseResult<Integer> setCheck(@RequestBody MesMaterialApplyVo mesMaterialApplyVo) {
        return BaseResult.success(mesMaterialApplyService.checkMesMaterialApply(mesMaterialApplyVo));
    }
}
